*******************************************************************
*Replication code for
*	Article: How COVID-19 affects voting for incumbents: evidence from local elections in France
*	Journal: PlosOne
*	Authors: Davide Morisi, Héloïse Cloléry, Guillaume Kon Kam King, and Max Schaub
*******************************************************************

*TESTING THE PARALLEL-TREND ASSUMPTION

*********
*create separate datasets

*2008
use "$pathdata\main_dataset_recoded.dta", clear
encode DepartmentCode, gen(dp)
gen pml = 0
replace pml = 1 if (DepartmentCode=="75") | (DepartmentCode=="13" & MunicipalityCode=="055SR01") | (DepartmentCode=="69" & MunicipalityCode=="123SR01")
label de pml 1 "Paris, Marseille, Lyon"
label val pml pml
drop if pml==1
gen parallel_sample = 0
replace parallel_sample = 1 if Incumbents_sample==1 & vote_share_incumbent_2008!=. & vs_2008incumbent_2014!=. & vote_share_Incumbent_2020!=. ///
& Incumbent_Name == incumbent2008_Name & Incumbent_Firstname == incumbent2008_Firstname
gen vote_trend = vote_share_incumbent_2008 if parallel_s==1
gen year = 2008
save "$pathdata\parallel_trend_2008.dta", replace

*2014
use "$pathdata\main_dataset_recoded.dta", clear
encode DepartmentCode, gen(dp)
gen pml = 0
replace pml = 1 if (DepartmentCode=="75") | (DepartmentCode=="13" & MunicipalityCode=="055SR01") | (DepartmentCode=="69" & MunicipalityCode=="123SR01")
label de pml 1 "Paris, Marseille, Lyon"
label val pml pml
drop if pml==1
gen parallel_sample = 0
replace parallel_sample = 1 if Incumbents_sample==1 & vote_share_incumbent_2008!=. & vs_2008incumbent_2014!=. & vote_share_Incumbent_2020!=. ///
& Incumbent_Name == incumbent2008_Name & Incumbent_Firstname == incumbent2008_Firstname
gen vote_trend = vs_2008incumbent_2014 if parallel_s==1
gen year = 2014
save "$pathdata\parallel_trend_2014.dta", replace

*2020
use "$pathdata\main_dataset_recoded.dta", clear
encode DepartmentCode, gen(dp)
gen pml = 0
replace pml = 1 if (DepartmentCode=="75") | (DepartmentCode=="13" & MunicipalityCode=="055SR01") | (DepartmentCode=="69" & MunicipalityCode=="123SR01")
label de pml 1 "Paris, Marseille, Lyon"
label val pml pml
drop if pml==1
gen parallel_sample = 0
replace parallel_sample = 1 if Incumbents_sample==1 & vote_share_incumbent_2008!=. & vs_2008incumbent_2014!=. & vote_share_Incumbent_2020!=. ///
& Incumbent_Name == incumbent2008_Name & Incumbent_Firstname == incumbent2008_Firstname
gen vote_trend = vote_share_Incumbent_2020 if parallel_s==1
gen year = 2020
save "$pathdata\parallel_trend_2020.dta", replace

*combine datasets
use "$pathdata\parallel_trend_2008.dta", clear
append using "$pathdata\parallel_trend_2014.dta"
append using "$pathdata\parallel_trend_2020.dta"


***************
*Testing the parallel trend assumption

global PathRES = "$pathtables\"
global controls_log log_PopDensity log_MaleToFemaleRatio log_shareImmigrants log_shareBlueCollar log_shareUnemp log_shareAbove65 MedianStandardLiving
xtile prevalence1_100_dum = prevalence1_100 if parallel_s==1, nq(2) // low vs high covid-19 spread

*
*Table S13. Testing parallel trend assumption
eststo clear
eststo, title("(1)"): reg vote_trend i.prevalence1_100_dum##ib2014.year if parallel_s==1, cluster(municip)
eststo, title("(2)"): reg vote_trend i.prevalence1_100_dum##ib2014.year $controls_log if parallel_s==1, cluster(municip)
esttab using "${PathRES}TableS13.tex", ///
b(%6.3f) se(%6.3f) starlevels(* .05 ** .01 *** .001)  scalars (r2) title("Table S13. Testing parallel assumption") compress noeqlines replace


*
*FIGURE S3

*Figure S3.A Visual inspection
	*parallel trend between 2008 and 2014 in low and high spread municipalities
	*but different trend between 2014 and 2020
egen low_spread = mean(vote_trend) if prevalence1_100_dum==1, by(year)
egen high_spread = mean(vote_trend) if prevalence1_100_dum==2, by(year)
twoway (scatter low_spread year, sort(year) connect(ascending) color(blue) xlabel(2008(6)2020, valuelabel) title("A) Average vote for incumbents""(Descriptive results)") ///
	ytitle("Average vote for incumbent") graphregion(color(white)) name(figS3_A, replace)) ///
	(scatter high_spread year, sort(year) connect(ascending) color(red) legend(pos(6)))

*Figure S3.B Regression model
eststo clear
reg vote_trend i.prevalence1_100_dum##ib2014.year $controls_log if parallel_s==1, cluster(municip)
eststo low_spread: margins year, at(prevalence1_100_dum=1) post
reg vote_trend i.prevalence1_100_dum##ib2014.year $controls_log if parallel_s==1, cluster(municip)
eststo high_spread: margins year, at(prevalence1_100_dum=2) post
coefplot low_spread high_spread, vert recast(line) graphregion(color(white)) ///
title("B) Average vote for incumbents""(Regression estimates)") ///
ytitle("Predicted vote for incumbent") name(figS3_B, replace)  legend(pos(6))
*note("Note: Estimates based on OLS regression model 2 in Table S13.""Vertical bars are 95% confidence intervals")

*Figure A2.C  Regression model
eststo clear
reg vote_trend i.prevalence1_100_dum##ib2014.year $controls_log if parallel_s==1, cluster(municip)
eststo low_spread2: margins, dydx(year) at(prevalence1_100_dum=1) post
reg vote_trend i.prevalence1_100_dum##ib2014.year $controls_log if parallel_s==1, cluster(municip)
eststo high_spread2: margins, dydx(year) at(prevalence1_100_dum=2) post
coefplot low_spread2 high_spread2, vert yline(0, lpattern(dash) lcolor(gs10)) graphregion(color(white)) ///
title("C) Difference in vote for incumbents""in 2008 and 2020 vs. 2014") ///
ytitle("Difference in vote for incumbents") name(figS3_C, replace) legend(pos(6))
*note("Note: Estimates based on OLS regression model 2 in Table S13.""Vertical bars are 95% confidence intervals")

*combined figure
graph combine figS3_A figS3_B figS3_C, graphregion(color(white))
graph save "Graph" "$pathfigures\PlosOne_FigS3.gph"
